package com.ruoyi.devicebind.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.devicebind.service.IdeviceunbindService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.devicebind.domain.devicebind;
import com.ruoyi.devicebind.service.IdevicebindService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 设备绑定解绑Controller
 * 
 * @author ruoyi
 * @date 2021-03-16
 */
@RestController
@RequestMapping("/devicebind")
public class devicebindController extends BaseController
{
    @Autowired
    private IdevicebindService devicebindService;

    @Autowired
    private IdeviceunbindService ideviceunbindService;

    /**
     * 查询设备列表
     */
    @PreAuthorize(hasPermi = "devicebind:devicebind:list")
    @GetMapping("/list")
    public TableDataInfo list(devicebind devicebind)
    {
        startPage();
        List<devicebind> list = devicebindService.selectdevicebindList(devicebind);
       // System.out.println("--------------");
      //  System.out.println(list);
        return getDataTable(list);
    }

    /**
     * 导出设备绑定解绑列表
     */
    @PreAuthorize(hasPermi = "devicebind:devicebind:export")
    @Log(title = "设备绑定解绑", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, devicebind devicebind) throws IOException
    {
        List<devicebind> list = devicebindService.selectdevicebindList(devicebind);
        ExcelUtil<devicebind> util = new ExcelUtil<devicebind>(devicebind.class);
        util.exportExcel(response, list, "devicebind");
    }

    /**
     * 获取设备绑定解绑详细信息
     */
    @PreAuthorize(hasPermi = "devicebind:devicebind:query")
    @GetMapping(value = "/{correctionuserId}")
    public AjaxResult getInfo(@PathVariable("correctionuserId") Long correctionuserId)
    {
        return AjaxResult.success(devicebindService.selectdevicebindById(correctionuserId));
    }

    /**
     * 新增设备绑定解绑
     */
    @PreAuthorize(hasPermi = "devicebind:devicebind:add")
    @Log(title = "设备绑定解绑", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody devicebind devicebind)
    {
        return toAjax(devicebindService.insertdevicebind(devicebind));
    }

    /**
     * 修改设备绑定解绑
     */
    @PreAuthorize(hasPermi = "devicebind:devicebind:edit")
    @Log(title = "设备绑定解绑", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody devicebind devicebind)
    {
        System.out.println("修改！！！");
        return toAjax(devicebindService.updatedevicebind(devicebind));
    }


    /**
     * 删除设备绑定解绑
     */
    @PreAuthorize(hasPermi = "devicebind:devicebind:remove")
    @Log(title = "设备绑定解绑", businessType = BusinessType.DELETE)
	@DeleteMapping("/{correctionuserIds}")
    public AjaxResult remove(@PathVariable Long[] correctionuserIds)
    {
        return toAjax(devicebindService.deletedevicebindByIds(correctionuserIds));
    }


    /**
     * 解绑设备
     */
    @PreAuthorize(hasPermi = "devicebind:devicebind:unbind")
    @Log(title = "设备解绑", businessType = BusinessType.UPDATE)
    @PutMapping("/{correctionuserIds}")
    public AjaxResult unBind(@PathVariable Long[] correctionuserIds)
    {
        return toAjax(devicebindService.unBind(correctionuserIds));
    }


    /**
     * 绑定设备
     */
    @PreAuthorize(hasPermi = "devicebind:devicebind:bind")
    @Log(title = "设备绑定", businessType = BusinessType.UPDATE)
    @PutMapping("/bind")
    public int bind(@RequestBody devicebind devicebind)             //0:绑定    1：未绑定   2：待绑定
    {

        //System.out.println("绑定！！！");
        int code = ideviceunbindService.bind(devicebind);
        System.out.println("code: "+ code);


        return code;

    }
}
